package com.example.designpartten.excel;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping("/excel")
@Slf4j
public class WriteExcel {

	@Autowired
	private JdbcTemplate jdbcTemplate;


	@RequestMapping("/write")
	public void getDbType() {
		String sql = "SELECT * from entrust_register a where a.ENTRUST_DATE  like '2019年%' or (a.ENTRUST_DATE like '2020年%') ORDER BY a.CREATE_DATE asc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		List<ExcelModel> excelModels = Lists.newArrayListWithCapacity(list.size());

		for (Map<String, Object> map : list) {
			Set<Map.Entry<String, Object>> entries = map.entrySet();
			ExcelModel model = new ExcelModel();
			for (Map.Entry<String, Object> entry : entries) {
				String key = entry.getKey();
				Object value = entry.getValue();
				String father = "";
				String mother = "";
				List<String> children = Lists.newArrayList();
				if ("ID".equals(key)) {
					String abstractSql = "SELECT * from entrust_abstracts a where a.REGISTER_ID = " + value;
					List<Map<String, Object>> abstractList = jdbcTemplate.queryForList(abstractSql);
					for (Map<String, Object> abstractMap : abstractList) {
						Set<Map.Entry<String, Object>> abstractEntries = abstractMap.entrySet();
						for (Map.Entry<String, Object> abstractEntry : abstractEntries) {
							String abstractKey = abstractEntry.getKey();
							Object abstractValue = abstractEntry.getValue();



							/*if ("specimen_Code".equals(abstractKey) && abstractKey.contains("-F")) {
								father = abstractEntry.get
							}
							if ("specimen_Code".equals(abstractKey) && abstractKey.contains("-M")) {
								mother = String.valueOf(abstractValue);
							}
							if ("specimen_Code".equals(abstractKey) && abstractKey.contains("-C")) {
								children.add(String.valueOf(abstractValue));
							}*/
						}
					}

				}

				//areaCode 默认是空
				//案件流水号  赣(景)[2020]物鉴字第1794号
				if ("CODE".equals(key)) {
					model.setSerialNum("赣(景)[" + getYear(String.valueOf(value)) + "]物鉴字第" + getNum(String.valueOf(value)) + "号");
				}
				//委托人姓名
				if ("CLIENT_NAME".equals(key)) {
					model.setClientName(String.valueOf(value));
				}

				//送检人姓名
				if ("AGENT_NAME".equals(key)) {
					model.setAgentName(String.valueOf(value));
				}

				//送检人证件号码
				model.setAgentId("空");

				//送检人移动电话号码
				if ("AGENT_TEL".equals(key)) {
					model.setAgentTel(String.valueOf(value));
				}

				//送检日期
				if ("ENTRUST_DATE".equals(key)) {
					model.setCheckDate(String.valueOf(value));
				}

				//委托人主体名称
				if ("CLIENT_NAME".equals(key)) {
					model.setClientOrgName(String.valueOf(value));

					//委托人主体单位
					if (String.valueOf(value).length() > 4) {
						model.setClientOrg("公安机关委托");
					} else {
						model.setClientOrg("个人委托");
					}
				}
				//委托鉴定类别
				model.setClientType("0104");
				//鉴定委托事项
				model.setCheckMessage("");

			}

			excelModels.add(model);
		}
		// 这里 需要指定写用哪个class去写
		ExcelWriter excelWriter = null;
		try {
			excelWriter = EasyExcel.write("fileName", ExcelModel.class).build();
			WriteSheet writeSheet = EasyExcel.writerSheet("鉴定").build();
			excelWriter.write(excelModels, writeSheet);
		} finally {
			// 千万别忘记finish 会帮忙关闭流
			if (excelWriter != null) {
				excelWriter.finish();
			}
		}
	}


	private String getYear(String code) {
		return code.substring(2, 6);
	}

	private String getNum(String code) {
		String num = code.substring(code.length() - 4);
		if ("0".equals(num.substring(0, 1))) {
			return code.substring(code.length() - 3);
		}
		return num;
	}


	private String getCheckMessage(String father, String mother, List<String> children) {
		if (StringUtils.isNotEmpty(father) && StringUtils.isNotEmpty(mother)) {

		}

		return "";
	}


}
